﻿using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace MathService.Functions.Plotter.GraphComponents
{
    public class Graph : UserControl, IGraphElement
    {
        private readonly Container components;
        private int graduationsX = 5;
        private int graduationsY = 5;
        private Color graphAreaColor = Color.White;
        private Color gridlineColor = Color.LightGray;
        private GridStyles gridlines = GridStyles.Grid;
        private byte transparency = 100;
        private string valueFormat = "";
        private Color xAxisColor = Color.Black;
        private Color yAxisColor = Color.Blue;

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
         Description("No. of graduations on the X axis"), Category("Appearance"), Browsable(true)]
        public int GraduationsX
        {
            get { return graduationsX; }
            set
            {
                graduationsX = value;
                RefreshDisplay();
            }
        }

        [Description("No. of graduations on the Y axis"), Category("Appearance"), Browsable(true),
         DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public int GraduationsY
        {
            get { return graduationsY; }
            set
            {
                graduationsY = value;
                RefreshDisplay();
            }
        }

        [Browsable(false)]
        public Rectangle GraphArea { get; set; }

        [Description("Color of the graph's area"),
         DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), Browsable(true),
         Category("Appearance")]
        public virtual Color GraphAreaColor
        {
            get { return graphAreaColor; }
            set
            {
                graphAreaColor = value;
                RefreshDisplay();
            }
        }

        [Description("Color of the grid lines"),
         DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), Browsable(true),
         Category("Appearance")]
        public Color GridlineColor
        {
            get { return gridlineColor; }
            set
            {
                gridlineColor = value;
                RefreshDisplay();
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), Category("Appearance"),
         Description("Style of the gridlines"), Browsable(true)]
        public virtual GridStyles Gridlines
        {
            get { return gridlines; }
            set
            {
                gridlines = value;
                RefreshDisplay();
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible), Browsable(true),
         Category("Appearance"),
         Description(
                     "The amount of transparency in the bar. Values range from 0 to 255. 0 is fully transparent, 255 is opaque."
                     )]
        public byte Transparency
        {
            get { return transparency; }
            set
            {
                transparency = value;
                RefreshDisplay();
            }
        }

        [Category("Appearance"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
         Description("The .NET style of format for displaying the value. Eg. {0:F}, {0:E} etc"), Browsable(true)]
        public string ValueFormat
        {
            get { return valueFormat; }
            set
            {
                valueFormat = value;
                RefreshDisplay();
            }
        }

        [Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
         Category("Appearance"), Description("Color of the X axis")]
        public virtual Color XAxisColor
        {
            get { return xAxisColor; }
            set
            {
                xAxisColor = value;
                RefreshDisplay();
            }
        }

        [Description("Color of the Y axis"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
         Category("Appearance"), Browsable(true)]
        public virtual Color YAxisColor { get { return yAxisColor; } set { yAxisColor = value; } }

        public Graph() { }
        public Graph(Container container) { components = container; }

        #region IGraphElement Members

        public virtual void Draw(Graphics graphics) { }

        #endregion

        protected override void Dispose(bool disposing)
        {
            if(disposing && (components != null))
                components.Dispose();
            base.Dispose(disposing);
        }

        protected void RefreshDisplay()
        {
            Invalidate();
            Update();
        }
    }
}
